<?php
/**
 * 图片处理
 * @authors ak (duguyi187@163.com)
 * @date    2017-01-10 17:56:23
 * @version $Id$
 */

//调用文章的全部图片
function all_img($content){

	// Loop through each image in each gallery
	$image_list = '<div id="gallery-1"><figure class="gallery-item">';
	foreach( $content as $image_url ) {
            //去除br
            $image_list .= '<a href="'.$image_url.'" data-gallery=""><img width="30%" src="'.$image_url.'"/> </a>';
	}
	$image_list .="</figure></div>";
	echo $image_list;
}


/**
 * 相册
 */
function echo_first_image($postID) {
    $attachments = get_attached_media('image', $postID);
    if ($attachments) {
        echo '<ul class="images">';
        foreach ($attachments as $attachment) {
            $image_attributes = wp_get_attachment_image_src($attachment->ID, 'thumbnail')  ? wp_get_attachment_image_src($attachment->ID, 'thumbnail') : wp_get_attachment_image_src($attachment->ID, 'full');
            echo '<li>';
            echo '<img data-original="'.wp_get_attachment_url($attachment->ID).'" src="' . wp_get_attachment_thumb_url($attachment->ID) . '" alt="'.get_the_title($postID).'" class="current">';
            if (naruco_get_attachment_field($attachment->ID)!=null&&naruco_get_attachment_field($attachment->ID)!="") {
                echo '<p>'.naruco_get_attachment_field($attachment->ID).'</p>';
            }else{
                echo '<p>'.get_the_title($postID).'</p>';
            }
            echo "</li>";
        }
        echo '</ul>';
    }
}

/**
 * 相册第一张
 */
function echo_first_image_one($postID) {
    $attachments = get_attached_media('image', $postID);
    if ($attachments) {
        foreach ($attachments as $attachment) {
            echo wp_get_attachment_url($attachment->ID);
            break;
        }
    }
}

function naruco_get_attachment_field($attachment_id='', $field_name='caption' ){
    if ( empty( $attachment_id ) )
        return null;
    $args = array(
        'include'    => $attachment_id,
        'post_status' => 'inherit',
        'post_type'    =>    'attachment',
        'post_mime_type' => 'image'
    );
    $attachments = get_posts( $args );
    if ( empty($attachments) )
        return '';
    $attachment = array_shift($attachments);
    //var_dump($attachment);
    switch($field_name){
        case 'caption':
            return $attachment->post_excerpt;
        case 'description':
            return $attachment->post_content;
        default:
            return $attachment->post_title;
    }
}

/*--------------------------------------
自动为第一张图片添加链接地址，其他图片只加alt属性，不加链接
默认链接地址为当前文章的链接，alt属性为当前文章的标题,并且全局添加 class=\"img-responsive aligncenter\"
通过修改判断语句if($count==1)，可以为指定顺序图片添加链接，不局限于第一个图片
--------------------------------------*/
$count = 0;
function auto_image_handler($matches){
    global $count,$post;
    $count++;
    if($count==1){//第一个图片添加链接地址
        return "<a href=\"".get_permalink()."\" title=\"".$post->post_title."\" ><img class=\"img-responsive aligncenter\" src=\"$matches[2]\" alt=\"".$post->post_title."\" /></a>";
    }
    else{//其他图片添加alt属性
        return "<img class=\"img-responsive aligncenter\" src=\"$matches[2]\" alt=\"".$post->post_title."\" /></a>";
    }
}
function auto_post_link($content){
    $content = preg_replace_callback('/<\s*img\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i',
    'auto_image_handler',$content);
    return $content;
}
add_filter ('the_content', 'auto_post_link',0);


//获取文章的第一张图片地址
function catch_that_image() {
    global $post, $posts;
    $first_img = '';
    ob_start();
    ob_end_clean();
    $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
    $first_img = $matches[1][0];
    if(empty($first_img)){
        $first_img = "/images/default.jpg";
    }
    return $first_img;
}